package com.ow.dva.module.park.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ow.dva.config.WXSCPayConfig;
import com.ow.dva.module.account.entity.AccountInfo;
import com.ow.dva.module.account.service.AccountInfoService;
import com.ow.dva.module.base.service.ImageService;
import com.ow.dva.module.duty.entity.Duty;
import com.ow.dva.module.duty.service.DutyService;
import com.ow.dva.module.park.entity.JsonRoad;
import com.ow.dva.module.roadCode.entity.RoadCode;
import com.ow.dva.module.roadCode.service.RoadCodeService;
import com.ow.dva.util.QRCode.ZXingBackGroundUtils;
import com.ow.dva.util.UUIDFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import com.ow.dva.module.base.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.ow.dva.module.park.entity.Road;
import com.ow.dva.module.park.service.RoadService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import com.ow.dva.module.base.entity.json.RT;
import org.springframework.ui.Model;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import com.ow.dva.module.base.entity.param.DvaPage;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 车道表 前端控制器
 * </p>
 *
 * @author zsj
 * @version V1.0
 * @since 2020-04-08
 */
@Controller
@Api(tags = "车道表 相关控制器")
@RequestMapping("/road")
public class RoadController extends BaseController {

    /**
     * 分页获取车道表数据列表信息
     * 创建时间：2020-04-08 zsj
     * 修改时间：2020-04-08 zsj 加注释
     * 修改时间：
     */
    @ApiOperation(value = "分页获取", notes = "批量获取车道表信息列表，分页，可携带条件")
    @RequiresPermissions("road:page")
    @GetMapping(value = "/page")
    public String page(DvaPage<Road> dvaPage, @RequestParam Map<String, Object> param, Model model) {

        DvaPage<Road> page = roadService.page(dvaPage, param);
        model.addAttribute("page", page);
        model.addAttribute("param", param);
        return "road/list";
    }

    /**
     * 进入车道表添加页面。
     * 创建时间：2020-04-08 zsj
     * 修改时间：2020-04-08 zsj 加注释
     * 修改时间：
     */
    @ApiOperation(value = "进入添加页面", notes = "进入车道表添加页面")
    @RequiresPermissions("road:add")
    @GetMapping(value = "/add")
    public String add() {

        return "road/add";
    }

    /**
     * 添加车道表信息。
     * 创建时间：2020-04-08 zsj
     * 修改时间：2020-04-08 zsj 加注释
     * 修改时间：
     */
    @ApiOperation(value = "添加", notes = "添加单个车道表信息")
    @RequiresPermissions("road:add")
    @PostMapping("/save")
    @ResponseBody
    public RT save(Road road) {

        return roadService.save(road) ? RT.ok() : RT.error(RT.ERROR_0101);
    }

    /**
     * 进入车道表编辑页面。
     * 创建时间：2020-04-08 zsj
     * 修改时间：2020-04-08 zsj 加注释
     * 修改时间：
     */
    @ApiOperation(value = "进入编辑页面", notes = "进入车道表编辑页面")
    @RequiresPermissions("road:edit")
    @GetMapping(value = "/edit")
    public String edit(String id, Model model) {

        model.addAttribute("data", roadService.getById(id));

        return "road/edit";
    }

    /**
     * 修改车道表信息。
     * 创建时间：2020-04-08 zsj
     * 修改时间：2020-04-08 zsj 加注释
     * 修改时间：
     */
    @ApiOperation(value = "修改", notes = "修改单个车道表信息")
    @RequiresPermissions("road:edit")
    @PostMapping("/update")
    @ResponseBody
    public RT update(Road road) {

        return roadService.updateById(road) ? RT.ok() : RT.error(1);
    }

    /**
     * 删除单个车道表信息。
     * 创建时间：2020-04-08 zsj
     * 修改时间：2020-04-08 zsj 加注释
     * 修改时间：
     */
    @ApiOperation(value = "删除", notes = "删除单个车道表信息")
    @RequiresPermissions("road:delete")
    @PostMapping("/delete")
    @ResponseBody
    public RT delete(@ApiParam(name = "要删除的车道表ID") String id) {

        return roadService.removeById(id) ? RT.ok() : RT.error(1);
    }

    /**
     * 批量删除多个车道表信息。
     * 创建时间：2020-04-08 zsj
     * 修改时间：2020-04-08 zsj 加注释
     * 修改时间：
     */
    @ApiOperation(value = "批量删除", notes = "批量删除多个车道表信息")
    @RequiresPermissions("road:delete")
    @PostMapping("/deleteByIds")
    @ResponseBody
    public RT deleteByIds(@ApiParam(name = "要删除的车道表ID List") @RequestParam List<String> ids) {

        return roadService.removeByIds(ids) ? RT.ok() : RT.error(1);
    }


    /**
     * @param model
     * @return java.lang.String
     * @author 姜延鹏
     * @Description 设置值班
     * @date 2020/4/7
     */
    @GetMapping(value = "/setOnDuty")
    public String setOnDuty(Model model, String roadId) {
        List<AccountInfo> accountInfos = accountInfoService.list(new QueryWrapper<AccountInfo>().eq("type", 1));
        model.addAttribute("accountInfos", accountInfos);
        model.addAttribute("roadId", roadId);
        return "road/setOnDuty";
    }

    @RequestMapping(value = "/getDuty")
    @ResponseBody
    public RT getDuty(String roadId) {
        List<Duty> dutyList = dutyService.getByRoadId(roadId);
        return RT.ok(dutyList);
    }


    @PostMapping("/saveDuty")
    @ResponseBody
    public RT saveDuty(String accountInfoId, String week, String timeInterval, String roadId) {

        Duty duties = new Duty();
        duties.setAccountInfoId(accountInfoId);
        duties.setWeek(Integer.parseInt(week));
        duties.setTimeInterval(Integer.parseInt(timeInterval));
        duties.setRoadId(roadId);
        dutyService.removeByRoadId(duties);
        String roadName = "";

        try {
            Road road = roadService.getById(roadId);
            if (road != null) {
                roadName = road.getRoadName();
            }
            duties.setId(UUIDFactory.getStringId());
            duties.setRoadName(roadName);
            AccountInfo accountInfo = accountInfoService.getById(duties.getAccountInfoId());
            if (accountInfo != null) {
                String accountInfoName = accountInfo.getName();
                duties.setAccountInfoName(accountInfoName);
            }
            dutyService.save(duties);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RT.ok();
    }

    /**
     * @param roadId
     * @return com.ow.dva.module.base.entity.json.RT
     * @author 姜延鹏
     * @Description 生成二维码
     * @date 2020/4/10
     */
    @RequestMapping(value = "/qrCode")
    @ResponseBody
    public RT qrCode(String roadId) {
        String staticPath = "";
        String relativePath = "";
        staticPath = staticImageService.getStaticPath("/upload");
        relativePath = staticImageService.generateImageFileName(staticPath, "image/jpeg");

        String codeUrl = "http://192.168.1.58:8088/appServerSettings/dutyCode?roadId=" + roadId;

        Road road = roadService.getById(roadId);
        JsonRoad json = new JsonRoad();

        json.setId(roadId);
        json.setDirectionType(road.getDirectionType());
        json.setOpenStatus(road.getOpenStatus());
        json.setParkId(road.getParkId());
        json.setParkName(road.getParkName());
        json.setRoadCode(road.getRoadCode());
        json.setRoadName(road.getRoadName());
        json.setTerminalIp(road.getTerminalIp());
        json.setTerminalName(road.getTerminalName());
        json.setSentryBoxCod(road.getSentryBoxCode());

        String roadJson = JSONObject.toJSONString(json);

        boolean b = ZXingBackGroundUtils.drawLogoQRCode(staticPath + "/src/main/resources/static/images/rt.jpg", staticPath + relativePath, roadJson);
        if (b) {
            List<RoadCode> roadCodes = roadCodeService.selectByRoadId(roadId);
            if (roadCodes.size() > 0) {
                roadCodeService.removeByRoadId(roadId);
            }
            RoadCode roadCode = new RoadCode();
            roadCode.setId(UUIDFactory.getStringId());
            roadCode.setRoadId(roadId);
            roadCode.setImagePath(relativePath);
            roadCode.setCreateTime(new Date());
            roadCodeService.save(roadCode);
            return RT.ok();
        } else {
            return RT.error(2, "值班二维码生成失败");
        }
    }

    @RequestMapping(value = "/showCode")
    public String showCode(String roadId, Model model) {
        List<RoadCode> roadCodes = roadCodeService.selectByRoadId(roadId);
        String imagePath = roadCodes.get(0).getImagePath();
        model.addAttribute("imagePath", imagePath);
        return "/road/showCode";
    }

    /**
     * 获取车道支付二维码
     *
     * @param roadId
     * @param model
     * @return
     */
    @RequestMapping(value = "/getRoadPayCode")
    public String getRoadPayCode(String roadId, Model model) {
        model.addAttribute("roadId", roadId);
        return "/road/roadPayCode";
    }

    @Resource
    RoadService roadService;
    @Resource
    private AccountInfoService accountInfoService;
    @Resource
    private DutyService dutyService;
    @Resource
    private ImageService imageService;

    private static ImageService staticImageService;

    @PostConstruct
    public void init() {
        staticImageService = imageService;
    }

    @Resource
    private RoadCodeService roadCodeService;
}
